package com.bahiaapk.services;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import blueprint.software.blueframe.exceptions.BlueFrameException;
import blueprint.software.blueframe.synchronize.HTTPConnect;
import com.bahiaapk.dto.News;
import com.bahiaapk.util.BahiaUtil;

public class SynchronizeUpdateNewsRanking implements Runnable {
	 
	 private Handler handler;
	 private Context context;
	 private HTTPConnect connect;
	 private News newsDetail;
	 private final Thread thread = new Thread(this);


	 public void synchronize(Context context, Handler handler, News newsDetail){
		 this.handler = handler;
		 this.context= context;
		 this.newsDetail = newsDetail;
		 thread.start();
	 }
	 
	 @Override
	 public void run() {
		Bundle data = new Bundle();
		
		if(BahiaUtil.haveNetworkConnection(context)){
			try{
				connect = new HTTPConnect(BahiaUtil.urlServer+"/setNewsRead");
				ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();		
		    	nameValuePairs.add(new BasicNameValuePair("id",newsDetail.getId().toString()));
		    	
		    	try {
					connect.setObject("", nameValuePairs);
				
				} catch (BlueFrameException e) {
					Log.e(BahiaUtil.TAG, e.toString());
				}
		    	
	    	}catch (Exception e) {
				Log.e(BahiaUtil.TAG, e.toString());
			}
		}
		
		data.putBoolean("status", true);
		
		// Add the data to a Message
		Message msg = new Message();
		msg.setData(data);
						
		// Send the Message with the data to the Handler
		handler.sendMessage(msg);		
	 }
}